/**
 * 字典管理API
 */
import type {
  CreateDictDataRequest,
  CreateDictTypeRequest,
  DictData,
  DictDataListParams,
  DictDataListResponse,
  DictType,
  DictTypeListParams,
  DictTypeListResponse,
  GetDictDataByCodeParams,
  UpdateDictDataRequest,
  UpdateDictTypeRequest
} from '@/types/dict'
import request from '@/utils/request'

// ============ 字典类型API ============

/**
 * 获取字典类型列表
 */
export const getDictTypeList = (params: DictTypeListParams) => {
  return request.get<DictTypeListResponse>('/dict/type/list', params)
}

/**
 * 根据ID获取字典类型
 */
export const getDictTypeById = (id: number) => {
  return request.get<DictType>(`/dict/type/${id}`)
}

/**
 * 创建字典类型
 */
export const createDictType = (data: CreateDictTypeRequest) => {
  return request.post<DictType>('/dict/type', data)
}

/**
 * 更新字典类型
 */
export const updateDictType = (id: number, data: UpdateDictTypeRequest) => {
  return request.put<DictType>(`/dict/type/${id}`, data)
}

/**
 * 删除字典类型
 */
export const deleteDictType = (id: number) => {
  return request.delete(`/dict/type/${id}`)
}

// ============ 字典数据API ============

/**
 * 获取字典数据列表
 */
export const getDictDataList = (params: DictDataListParams) => {
  return request.get<DictDataListResponse>('/dict/data/list', params)
}

/**
 * 根据字典编码获取字典数据（用于前端下拉框）
 */
export const getDictDataByCode = (params: GetDictDataByCodeParams) => {
  return request.get<DictData[]>('/dict/data/code', params)
}

/**
 * 根据ID获取字典数据
 */
export const getDictDataById = (id: number) => {
  return request.get<DictData>(`/dict/data/${id}`)
}

/**
 * 创建字典数据
 */
export const createDictData = (data: CreateDictDataRequest) => {
  return request.post<DictData>('/dict/data', data)
}

/**
 * 更新字典数据
 */
export const updateDictData = (id: number, data: UpdateDictDataRequest) => {
  return request.put<DictData>(`/dict/data/${id}`, data)
}

/**
 * 删除字典数据
 */
export const deleteDictData = (id: number) => {
  return request.delete(`/dict/data/${id}`)
}
